﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MVCPets.Extensions;
using System.IO;

namespace MVCPets.Tests.Extensions
{
  [TestClass]
  public class ListExtensions
  {
    private class TestData
    {
      public string Field1 { get; set; }
      public string Field2 { get; set; }
    }

    [TestMethod]
    public void Test_Can_Gen_CSV()
    {
      List<TestData> data = new List<TestData>();
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      string s = data.ToCSV();
      Assert.IsTrue(!string.IsNullOrEmpty(s));
    }

    [TestMethod]
    public void Test_Can_Write_File()
    {
      List<TestData> data = new List<TestData>();
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });

      File.WriteAllText("testfile", "");
      FileInfo fi = new FileInfo("testfile");


      data.WriteDelimitedFile(fi, ",");
      Assert.IsTrue(File.Exists("testfile"));
    }

    [TestMethod]
    public void Test_Can_Write_Excel_File()
    {
      List<TestData> data = new List<TestData>();
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });
      data.Add(new TestData() { Field1 = Guid.NewGuid().ToString(), Field2 = Guid.NewGuid().ToString() });

      File.WriteAllText("testfile", "");

      string s = data.ToExcel();
      Assert.IsTrue(!string.IsNullOrEmpty(s));
      using (FileStream fs = new FileStream("testexce", FileMode.CreateNew))
      using (StreamWriter sw = new StreamWriter(fs))
      {
        data.ToExcelFile((TextWriter)sw);
      }
    }
  }
}
